package net.lab1024.sa.admin.module.sast.rule.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import net.lab1024.sa.admin.module.sast.rule.dao.RuleDao;
import net.lab1024.sa.admin.module.sast.rule.domain.*;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartStringUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service
public class RuleService {

    @Resource
    private RuleDao ruleDao;

    @Resource
    private RuleReferenceService ruleReferenceService;

    @Resource
    private RuleExampleService ruleExampleService;
    private static final Set<String> VALID_CATEGORIES = Set.of(
            "language",
            "rule_type",
            "problem_severity",
            "detection_precision"
    );

    /**
     * 分页查询规则信息
     * @param queryForm
     * @return
     */
    public ResponseDTO<PageResult<RuleVO>> query(RuleQueryForm queryForm){
        Page<?> page= SmartPageUtil.convert2PageQuery(queryForm);
        List<RuleVO> list= ruleDao.query(page,queryForm);
        if(!list.isEmpty()){
            getRuleType(list);
        }
        PageResult<RuleVO> pageResult= SmartPageUtil.convert2PageResult(page,list);
        return ResponseDTO.ok(pageResult);
    }

    /**
     * 填充规则类型
     * @param list
     */
    public void getRuleType(List<RuleVO> list){
        List<String> uuids = list.stream()
                .map(RuleVO::getUuid)
                .toList();
        List<RuleType> ruleTypes = ruleDao.selectRuleTypesByUuids(uuids);
        Map<String, List<Integer>> ruleTypeMap = ruleTypes.stream()
                .collect(Collectors.groupingBy(RuleType::getRuleUuid,
                        Collectors.mapping(RuleType::getType, Collectors.toList())));
        list.forEach(ruleVO -> ruleVO.setRuleType(ruleTypeMap.getOrDefault(ruleVO.getUuid(), List.of())));
    }
    /**
     * 分组获取分类数量列表
     * @param category  暂定可以查询language  rule_type  problem_severity detection_precision
     * @return
     */
    public ResponseDTO<List<RuleCategoryCountVO>> getCategoryCountList(String category) {
        // 验证category参数有效性
        if (SmartStringUtil.isEmpty(category)) {
            return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "category参数不能为空");
        }
        if (!VALID_CATEGORIES.contains(category)) {
            return ResponseDTO.error(UserErrorCode.PARAM_ERROR, "category参数无效");
        }
        List<RuleCategoryCountVO> list =ruleDao.getCategoryCountList(category);
        return ResponseDTO.ok(list);
    }

    /**
     * 根据uuid查询规则详情
     * @param uuid
     * @return
     */
    public ResponseDTO<RuleDetailVO> getByUuid(String uuid){
        RuleDetailVO ruleDetailVO =ruleDao.getByUuid(uuid);
        if(ruleDetailVO==null){
            return ResponseDTO.error(UserErrorCode.DATA_NOT_EXIST,"数据不存在");
        }
        //查询外表获取参考和例子的数据
        ruleDetailVO.setReferences(ruleReferenceService.queryByRuleUuid(ruleDetailVO.getUuid()));
        ruleDetailVO.setExample(ruleExampleService.queryByRuleUuid(ruleDetailVO.getUuid()));
        return ResponseDTO.ok(ruleDetailVO);
    }

    public ResponseDTO<?> getRuleCount() {
        return ResponseDTO.ok(ruleDao.selectCount(null));
    }
}
